package ru.jexp.rcp.supply.beans.account;

import java.util.List;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

import ru.jexp.izdelie.scet.IStrokaDokumenta;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.wizard.JExpWizard;

public class WizardPageDocStr<TypeDoc extends IStrokaDokumenta> extends
		WizardPage implements ICheckStateListener {

	public static final int WIZARD_DOC_STR_PAGE_CREATED = -51;
	public static final int WIZARD_DOC_STR_PAGE_CHANGE_CHECK = -53;

	public static <ParamTypeDoc extends IStrokaDokumenta> WizardPageDocStr<ParamTypeDoc> createMaterial() {
		return new WizardPageDocStr<ParamTypeDoc>(
				JExpSupplyMessages.Account_wizard_material_pagename);
	}

	public static <ParamTypeDoc extends IStrokaDokumenta> WizardPageDocStr<ParamTypeDoc> createPki() {
		return new WizardPageDocStr<ParamTypeDoc>(
				JExpSupplyMessages.Account_wizard_pki_pagename);
	}

	public static <ParamTypeDoc extends IStrokaDokumenta> WizardPageDocStr<ParamTypeDoc> createDetail() {
		return new WizardPageDocStr<ParamTypeDoc>(
				JExpSupplyMessages.Account_wizard_detail_pagename);
	}

	private CheckboxTableViewerWrapDocStr viewer;

	private WizardPageDocStr(String title) {
		super(title);
		setTitle(title);
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FillLayout());
		viewer = new CheckboxTableViewerWrapDocStr(composite);
		viewer.addCheckStateListener(this);

		setControl(composite);

		getWizardAccount().fireWizardListeners(WIZARD_DOC_STR_PAGE_CREATED);
	}

	public void setInputDocStr(List<TypeDoc> docStr) {
		viewer.setInput(docStr);
	}

	public void setCheckedDocStr(List<TypeDoc> docStr) {
		viewer.setCheckedElements(docStr.toArray());
		getWizardAccount()
				.fireWizardListeners(WIZARD_DOC_STR_PAGE_CHANGE_CHECK);

	}

	@SuppressWarnings("unchecked")
	public List<TypeDoc> getCheckedElements() {
		return (List<TypeDoc>) viewer.getCheckedElements();
	}

	@Override
	public void checkStateChanged(CheckStateChangedEvent event) {
		getWizardAccount()
				.fireWizardListeners(WIZARD_DOC_STR_PAGE_CHANGE_CHECK);
	}

	private JExpWizard getWizardAccount() {
		return (JExpWizard) getWizard();
	}

}
